﻿<%@ Page Title="" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server">

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link rel="stylesheet" href="../assets/css/system/GroupUser.css" />
    <link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <div class="btns-container"></div>
            <div class="right-content-box">
                <div class="content-container">
                    <div class="header-tips">设置数据权限及用户</div>
                    <div class="control-box">
                        <p>
                            <label class="left-title">数据组列表</label>
                            <label class="left-title">待选省份列表</label>
                            <label class="left-title">待选用户列表</label>
                        </p>
                    </div>

                    <div class="chooseArea">

                        <div class="leftArea1">
                            <div id="groupdata"></div>
                        </div>
                        <div class="leftArea1">
                            <div id="DataList"></div>
                        </div>
                        <div class="areaOutbox" style="width: 280px;">
                            <div class="leftArea1" style="width: 280px;">

                                <div id="UserList"></div>


                            </div>
                            <div class="searchbox">
                                <div class="input-group">
                                    <input type="text" class="form-control filter-users" placeholder="请输入用户名">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default btn-primary btn-filter-users" type="button">筛选</button>
                                    </span>
                                </div>
                                <!-- /input-group -->
                            </div>
                        </div>


                    </div>
                </div>



            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/js/page/rScroll.js"></script>
    <script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
    <script>

        $(document).ready(function () {
            layer.msg('玩命加载中……', { icon: 16, shade: 0.01, time: 0, maxWidth: 200 });


            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");

            //获取按钮
            commonUtil.loadBtnList(ModuleID, function () {
                rScroll();
            });

            commonUtil.loadDict2({ DictValue: "datagroup" }, function (json) {

                var newdata = $.map(json, function (obj) {
                    obj.id = obj.id || obj.ID.toString();

                    if (obj.DictValue == "datagroup") {
                        obj.state = { opened: true, disabled: true };
                        obj.parent = "#";
                    }
                    else {
                        obj.parent = obj.ParentId.toString();
                        obj.state = { opened: true };
                    }

                    obj.li_attr = { DictValue: obj.DictValue };
                    obj.text = obj.text || obj.DictName;

                    return obj;
                });
              console.log(newdata);

                //数据组列表
                $('#groupdata').jstree({
                    'core': {
                        data: newdata
                    }, "types": {
                        "default": {
                            "icon": "fa fa-folder text-info"
                        },
                        "child": {
                            "icon": "fa fa-file-text-o text-info"
                        }
                    },
                    "plugins": ["types"]

                }).on("ready.jstree", function () {
                    layer.closeAll('dialog');
                    // console.log($('#groupdata').jstree("selecct_node", newdata.))
                    var selectitem = {};
                    $.each(newdata, function (i,obj) {
                        if(obj.DictValue=="allgroup")
                        {
                            selectitem = obj;
                        }
                    });
                    $('#groupdata').jstree("select_node", selectitem)
                }).on("changed.jstree", function (e, data) {
                    // console.log(data.selected)
                    layer.msg('玩命加载中……', { icon: 16, shade: 0.01, time: 0, maxWidth: 200 });
                    
                    GetCehckedDataList(data.node.li_attr.DictValue, function (json) {
                        layer.closeAll('dialog');
                       
                        if (!Y.checkNull(json)) {
                            var newdata = [];
                            $.each(json.AllDataList, function (i, obj) {
                                if (obj.length > 0) {
                                    var newdata2 = {};
                                    $.each(obj, function (i2, obj2) {
                                        
                                            var newdata3 = {};
                                            newdata3.id = obj2.ParentAreaID;
                                            newdata3.parent = obj2.DistrictID;
                                            newdata3.type = "child";
                                            newdata3.text = obj2.ParentAreaName;
                                            $.each(json.CheckDataList, function (ci, cv) {
                                                if (obj2.ParentAreaID == cv.Value)
                                                    newdata3.state = { selected: true };//设置选中
                                            });
                                            if (!Y.checkNull(newdata3.id)) {
                                            newdata.push(newdata3);
                                        }
                                    });
                                    newdata2.id = obj[0].DistrictID;
                                    newdata2.parent = "#";
                                    newdata2.type = "default";
                                    newdata2.text = obj[0].DistrictName;
                                    if (!Y.checkNull(newdata2.id)) {
                                        newdata.push(newdata2);
                                    }
                                   


                                }

                            });


                              console.log(newdata)
                            $('#DataList').jstree("destroy");
                            //加载待选列表
                            $('#DataList').jstree({
                                'core': {
                                    data: newdata

                                },
                                "checkbox": {
                                    "keep_selected_style": false
                                },
                                "types": {
                                    "default": {
                                        "icon": "fa fa-folder text-info"
                                    },
                                    "child": {
                                        "icon": "fa fa-file-text-o text-info"
                                    }
                                },
                                "plugins": ["checkbox", "types"]

                            }).on("ready.jstree", function () {
                                console.log("ready.jstree")
                                //数据组对应的用户
                                GetGroupUserList(data.node.li_attr.DictValue, function (json) {

                                    $.map(json, function (obj) {
                                        obj.id = obj.UserID;
                                        return obj;
                                    });
                                    $('#UserList').jstree("deselect_all");
                                    $('#UserList').jstree("select_node", json);//选择用户

                                });
                            })
                        }

                    });



                });
            });
            //获取数据组对应的用户
            var GetGroupUserList = function (GroupDataID, callback) {
                Y.API2('DataGroup', 'GetGroupUserList', {
                    GroupDataID: GroupDataID
                }, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        callback(data);
                    }
                    // console.log(data)
                });
            }
            //待选省份列表
            var GetCehckedDataList = function (GroupDataID, callback) {
                Y.API2('DataGroup', 'GetCehckedDataList', {
                    GroupDataID: GroupDataID
                }, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        callback(data);
                    }
                    // console.log(data)
                });
            }
            //所有用户列表
            var GetAllUserList = function (callback) {
                Y.API2('DataGroup', 'GetAllUserList', { Keys: " " }, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        callback(data);
                    }
                    //  console.log(data)
                });
            }
            GetAllUserList(function (json) {
                var newdata = $.map(json, function (obj) {

                    obj.id = obj.UserID;
                    obj.parent = "#";

                    obj.text = obj.UserName + "【" + obj.UserCode + "】";
                    obj.state = { opened: true, selected: false };
                    obj.icon = "fa fa-user text-info";
                    return obj;
                });
                $('#UserList').jstree({
                    'core': {
                        data: newdata
                    },
                    "checkbox": {
                        "keep_selected_style": false
                    },
                    "search": {
                        show_only_matches: true
                    },
                    "plugins": ["checkbox", "search"]

                })

            });

            //提交
            $(document).on("click", "#btn-save", function () {
                var userList = $('#UserList').jstree("get_selected");
                var arealist = $('#DataList').jstree("get_bottom_selected", true);

                console.log(arealist)
                var _arealist=[];
                var i=0;
                $.map(arealist, function (obj) {
                    var l={};
                    l.DistrictID = obj.parent;
                    l.AreaID = obj.id;
                    _arealist[i]=l;
                    i++;
                    return obj;
                });
               
                //[{DistrictID:"",AreaID:""},{DistrictID:"",AreaID:""},{DistrictID:"",AreaID:""}]
                var groupData  = $('#groupdata').jstree("get_selected",true);
             
                console.log(userList)
                console.log(groupData)
                
                if (Y.checkNull(groupData))
                {
                    swal("请选择数据权限组", '', 'error');
                    return;
                }
                Y.API2('DataGroup', 'ModifyGroup', {
                    GroupDataID: groupData[0].li_attr.DictValue,
                    Arealist: _arealist,
                    UserList: userList
                }, function (data) {
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                        //  layer.closeAll('dialog');
                    } else {
                        swal("提交成功！", '', 'success');
                    }
                })


            });
            //过虑 筛选用户
            $(".btn-filter-users").on("click", function () {
                var v = $(".filter-users").val();
                $('#UserList').jstree("search", v);

            });
            var to = false;
            $('.filter-users').on("keyup",function () {
                var v = $(".filter-users").val();
                if (to) { clearTimeout(to); }
                to = setTimeout(function () {
                    $('#UserList').jstree("search", v);
                }, 250);
            });

            //自动高 DOCUMENT高减按钮栏+其它
            $(".chooseArea .leftArea1").height($(document).height() - (35 + 145));
        })
    </script>
</asp:Content>

